package com.hspedu.threaduse;

import java.util.TreeMap;

//使用多线程，模拟三个窗口同时售票
public class SellTicket {
    public static void main(String[] args) {
//        SellTicket01 sellTicket = new SellTicket01();
//        SellTicket01 sellTicket1 = new SellTicket01();
//        SellTicket01 sellTicket2 = new SellTicket01();
//        sellTicket.start();
//        sellTicket1.start();
//        sellTicket2.start();

        SellTicket02 sellTicket11 = new SellTicket02();
        SellTicket02 sellTicket12 = new SellTicket02();
        SellTicket02 sellTicket13 = new SellTicket02();
        new Thread(sellTicket11).start();
        new Thread(sellTicket12).start();
        new Thread(sellTicket13).start();

    }

}

class SellTicket01 extends Thread {
    private static int ticketNum=100;

    @Override
    public void run() {

        while (true) {
            if (ticketNum<=0){
                System.out.println("售票结束...");
                break;
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("窗口"+Thread.currentThread().getName()+"售出一张票"
            +"剩余票数="+(--ticketNum));

        }
    }
}

//实现接口的方式
class SellTicket02 implements Runnable{
    private  int ticketNum=100;

    @Override
    public void run() {

        while (true) {
            if (ticketNum <= 0) {
                System.out.println("售票结束...");
                break;
            }

            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票"
                    + "剩余票数=" + (--ticketNum));
        }
    }
}